package demo_lock;

import Interfaces.StudentImpl;
import bean.StudentBean2;

import java.util.ArrayList;
import java.util.Random;

/**
 * @author: android 超级兵
 * @create: 2022-01-17 16:55
 * TODO 读写锁
 **/
public class ReadWriteLockClient {

    private ArrayList<String> nameList = new ArrayList<String>() {
        {
            add("张三");
            add("王五");
            add("李四");
            add("小明");
            add("小王");
            add("小李");
            add("小张");
            add("小石");
        }
    };

    private class GetThread extends Thread {
        private final StudentImpl studentBean;

        public GetThread(StudentImpl studentBean) {
            this.studentBean = studentBean;
        }

        @Override
        public void run() {
            super.run();
            long start = System.currentTimeMillis();
            try {
                for (int i = 0; i < 10; i++) {
                    String name = studentBean.getName();
                    int age = studentBean.getAge();
//                    System.out.println("getThread:name" + name + "\tage:" + age);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println(Thread.currentThread().getName() + "读取商品数据耗时："
                        + (System.currentTimeMillis() - start) + "ms");
            }
        }
    }

    private class SetThread extends Thread {
        private final StudentImpl studentBean;
        private final Random random;

        public SetThread(StudentImpl studentBean) {
            this.studentBean = studentBean;
            random = new Random();
        }

        @Override
        public void run() {
            super.run();
            long start = System.currentTimeMillis();
            try {
                // 模拟网络实际情况
                for (int i = 0; i < 10; i++) {
                    // 随机一个名字
                    String name = nameList.get(random.nextInt(nameList.size()));
                    // 随机年龄 (20-60)
                    int age = random.nextInt(60 - 20) + 20;
                    Thread.sleep(50);

                    studentBean.setName(name);
                    studentBean.setAge(age);
//                    System.out.println("set数据为:name:" + name + "\tage:" + age);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println(Thread.currentThread().getName()
                        + "\t 写入数据耗时：" + (System.currentTimeMillis() - start) + "ms  -------------------");
            }
        }
    }


    private void build() throws InterruptedException {

        // synchronized 思路
//        StudentBean1 studentBean = new StudentBean1();

        // 读写锁思路
        StudentBean2 studentBean = new StudentBean2();

        for (int i = 0; i < 3; i++) {
            SetThread setThread =
                    new SetThread(studentBean);

            setThread.setName("set:" + i);
            setThread.start();

            for (int j = 0; j < 30; j++) {
                GetThread getThread =
                        new GetThread(studentBean);

                getThread.setName("get:" + i);
                getThread.start();
            }

            Thread.sleep(100);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new ReadWriteLockClient().build();
    }
}
